package com.yingli.storage.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DBDemo extends SQLiteOpenHelper {

    //数据库的版本号 如果数据库要升级，则升级的版本号，必须要比当前的数据库版本要高
    private static final int DB_VERSION = 2;

    private static final String DB_NAME = "student_db";

    private static final String S_TABLE = "student";
    public DBDemo(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public int insertStudent(String name,int age){
        //获取可写入的数据库对象
        SQLiteDatabase db = getWritableDatabase();
        //插入的键值对对象
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("age",age);
        db.insert(S_TABLE,null,values);
        return 1;
    }

    public long updateStudent(String name,int age){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",name);
        long result = db.update(S_TABLE,values,"age>?",new String[]{String.valueOf(age)});
        return result;

    }

    public List<Map<String,String>> getStusOlderThan(int a){
        //获取可读的数据库对象
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(S_TABLE,new String[]{"name","age"},"age>?",new String[]{String.valueOf(a)},null,null,null);
        List<Map<String,String>> datas = new ArrayList<>();
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
//            int id = cursor.getInt(cursor.getColumnIndex("id"));
            Map<String,String> map = new HashMap<>();
            map.put("name",name);
            map.put("age",String.valueOf(age));
//            map.put("id",String.valueOf(id));
            datas.add(map);
        }
        return datas;
    }

    public int deleteStudent(String age){
        SQLiteDatabase db = getWritableDatabase();
        return db.delete(S_TABLE,"age=?",new String[]{age});
    }

    public List<Map<String,String>> getAllStu(){
        //获取可读的数据库对象
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(S_TABLE,new String[]{"name","age"},null,null,null,null,null);
        List<Map<String,String>> datas = new ArrayList<>();
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
//            int id = cursor.getInt(cursor.getColumnIndex("id"));
            Map<String,String> map = new HashMap<>();
            map.put("name",name);
            map.put("age",String.valueOf(age));
//            map.put("id",String.valueOf(id));
            datas.add(map);
        }
        return datas;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table student(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER);");


    }

    //如果要更新数据库结构 比如新增表 修改数据库中的字段 需要调用此方法
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion==2) {
            db.execSQL("create table grade(_id INTEGER PRIMARY KEY AUTOINCREMENT,s_name TEXT,grade DOUBLE)");
        }
    }
}
